let html = require('html-webpack-plugin')
let { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
let path = require('path')
let webpack = require('webpack')
module.exports = {
  // entry: "./src/index.js",
  entry: {
    aaa: './src/index.js',
    bbb: './src/other.js'
  },
  output: {
    filename: "[name].[contenthash:8].js",
    path: path.resolve(__dirname, '../dist'),
    assetModuleFilename: 'imgs/[name][ext]'
  },
  plugins: [
    new CleanWebpackPlugin(),
    new html({
      filename: 'index.html',
      template: './public/index.html',
      chunks: ['aaa']
    }),
    new html({
      filename: 'other.html',
      template: './public/other.html',
      chunks: ['bbb']
    }),
    new MiniCssExtractPlugin({
      filename: 'index.css'
    }),
    // 定义变异阶段的全局变量
    new webpack.DefinePlugin({
      "WWW": "'hello'",
      QQQ: JSON.stringify("1+2"),
      "AAA.A": "'qweqew'",
      obj: {
        qqq: 123,
        www: "123"
      }
    })
  ],
  module: {
    /* loader的加载顺序是从右向左的 ，从下往上*/
    // css-loader 是把css模块翻译成webpack的有效模块；
    // style-loader是把翻译好的css文件变成对应的js，当这个js执行的时候
    // 把对应的css变成style标签插入到header当中
    rules: [
      {
        test: /\.(css|less)$/,
        use: [MiniCssExtractPlugin.loader, {
          loader: 'css-loader',
          options: {
            url: true,
          }
        }, 'less-loader', {
          loader: 'postcss-loader',
        }]
      },
      // {
      //   test: /\.less$/,
      //   use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader', 'postcss-loader']
      // },
      {
        test: /\.(jpg|jpeg|png|gif)$/,
        // use: [
        //   {
        //     loader: 'url-loader',
        //     options: {
        //       limit: 5 * 1024,// 资源大小不超过10kb的时候转成base64字符串
        //       name: 'imgs/[name].[ext]'
        //     },
        //   },
        // ],
        type: 'asset/resource',
      },
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: /node_modules/ // 排除node_modules中的js文件
      }

    ]
  }
}